package cn.ad.server.web.servlet;

import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.common.utils.SpringContextUtils;
import cn.ad.server.web.servlet.v2.RestPathFilter;
import cn.ad.server.web.spring.model.Exchange;
import cn.ad.server.web.spring.service.actions.AdTrackingAction;
import cn.ad.server.web.spring.service.actions.command.ClickAndImpressionCommand;
import cn.ad.server.web.spring.service.actions.presentation.ClickPresentation;
import cn.ad.server.web.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @deprecated 原广告请求处理Servlet框架将不推荐使用，统一采用v2包下的处理方式，请求入口参考{@link RestPathFilter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)}
 */
@Deprecated
@WebServlet("/redirect.html")
public class AdRedirectClickServlet extends AbstractAdServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2391659270221886692L;
	private Logger logger = LoggerFactory.getLogger(AdRedirectClickServlet.class);

	
	public AdTrackingAction getTrackingAction(){
		return SpringContextUtils.getBean(AdTrackingAction.class);
	}
	
	@Override
	void doService(HttpServletRequest request, HttpServletResponse response) {

		ClickPresentation cp = new ClickPresentation();

		try {

			ClickAndImpressionCommand clickCommand = new ClickAndImpressionCommand();
			WebUtils.convetGetRequestParam(request, clickCommand);

			if (Exchange.GDT.getValue().equals(clickCommand.getPlatform_id())) {
				covertGDTParam(clickCommand);
			}

			String ip = IPUtils.parseIp(request);
			clickCommand.setIp(ip);
			clickCommand.setUa(request.getHeader("user-agent"));
			clickCommand.setHandleType("3");

			getTrackingAction().handleClick(clickCommand, cp);

		} catch (Exception e) {
			logger.error("/redirect.html - error", e);
		} finally {
			loggerService.logAdClick(cp);
		}
	}
	
	
}
